package com.kmxd.ams.app.logs;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.logs.ILogArchiveUseSvc;
import com.kmxd.ams.client.logs.dto.LogArchiveUseAddDTO;
import com.kmxd.ams.client.logs.dto.LogArchiveUseQueryDTO;
import com.kmxd.ams.client.logs.vo.LogArchiveUseListVO;
import com.kmxd.ams.client.logs.vo.LogArchiveUseVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.logs.entity.LogArchiveUse;
import com.kmxd.ams.infra.logs.mapper.LogArchiveUseMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 档案利用日志 服务实现类
 *
 * @author nickbi
 * @since 2022-12-27
 */
@Service
@AllArgsConstructor
public class LogArchiveUseSvcImpl extends ServiceImpl<LogArchiveUseMapper, LogArchiveUse>
    implements ILogArchiveUseSvc {

  private LogArchiveUseMapper logArchiveUseMapper;

  @Override
  public Page<LogArchiveUseListVO> selectPage(Page reqPage, LogArchiveUseQueryDTO req) {
    LambdaQueryWrapper<LogArchiveUse> queryWrapper =
        QueryGen.init(new QueryWrapper<LogArchiveUse>(), req).lambda();
    Page<LogArchiveUse> page = logArchiveUseMapper.selectPage(reqPage, queryWrapper);
    Page<LogArchiveUseListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), LogArchiveUseListVO.class));
    return voPage;
  }

  @Override
  public List<LogArchiveUseListVO> selectList(LogArchiveUseQueryDTO req) {
    LambdaQueryWrapper<LogArchiveUse> queryWrapper =
        QueryGen.init(new QueryWrapper<LogArchiveUse>(), req).lambda();
    List<LogArchiveUse> list = logArchiveUseMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, LogArchiveUseListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(LogArchiveUseAddDTO req) {
    LogArchiveUse entity = BeanUtil.copyProperties(req, LogArchiveUse.class);
    logArchiveUseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(LogArchiveUseAddDTO req) {
    LogArchiveUse entity = BeanUtil.copyProperties(req, LogArchiveUse.class);
    logArchiveUseMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public LogArchiveUseVO view(Long id) {
    LogArchiveUse entity = logArchiveUseMapper.selectById(id);
    return BeanUtil.copyProperties(entity, LogArchiveUseVO.class);
  }
}
